package com.mofeng.myshop.domain.dto.base;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mofeng.myshop.until.MyUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SearchBaseMp {
    private String start;
    private String end;
    private int page;
    private int size;
    private String keyword;
    private String sort = "create_time,ASC";//create_time,ASC/asc;update_time,ASC/asc

    public <T> Page<T> getPageMp(Class<T> tClass) {
        if (this.page < 1) {
            this.page = 1;
        }
        if (this.size < 1) {
            this.size = 1;
        }
        Page<T> objectPage = Page.of(this.page, this.size);
        if (this.sort != null && !this.sort.isEmpty()) {
            List<String> sortList = new ArrayList<>();
            if (this.sort.contains(";")) {
                sortList = Arrays.asList(this.sort.split(";"));
            }
            for (String sort : sortList) {
                if (sort.contains(",")) {
                    String[] split = sort.split(",");
                    if (split[1].equalsIgnoreCase("asc")) {
                        objectPage.addOrder(OrderItem.asc(split[0]));
                    } else {
                        objectPage.addOrder(OrderItem.desc(split[0]));
                    }
                }
            }
        }
        return objectPage;
    }

    /**
     * 获取开始时间
     */
    public Date getStartTime() {
        return parseToDate(this.start);
    }

    /**
     * 获取结束时间
     */
    public Date getEndTime() {
        return parseToDate(this.end);
    }

    public String webNullHandle(String str) {
        if (MyUtil.StringUtil.isEmpty(str) || "null".equals(str) || "undefined".equals(str)) {
            return null;
        }
        return str;
    }
    public Long webNullHandle(Long number) {
        if (MyUtil.StringUtil.isEmpty(number)) {
            return null;
        }
        return number;
    }

    public String sqlKeyHandle(String str) {
        str = webNullHandle(str);
        if (str == null) {
            return null;
        }
        str = str.replace("%", "\\\\%");
        str = str.replace("_", "\\\\_");
        return "%" + str + "%";
    }

    public String sqlListHandle(List<String> strList) {
        StringBuilder content = new StringBuilder("( ");
        int i = 0;
        for (String str : strList) {
            i++;
            if (i == strList.size()) {
                content.append(str);
            } else {
                content.append(str).append(",");
            }
        }
        content.append(" )");
        return content.toString();
    }

    public String getKeyword() {
        return sqlKeyHandle(this.keyword);
    }

    private Date parseToDate(String dateString) {
        if (dateString == null || dateString.isEmpty()) {
            return null;
        }
        return parseToDate(dateString, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 字符串转换时间 支持 指定类型 和时间戳 解析时间
     *
     * @param dateString 时间字符串或者时间戳
     * @param pattern    解析格式 例如yyyy-MM-dd HH:mm:ss
     * @return
     */
    private Date parseToDate(String dateString, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            Date result = null;
            if (isNumber(dateString)) {
                result = new Date(Long.parseLong(dateString));
            } else {
                result = sdf.parse(dateString);
            }
            return result;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    private LocalDateTime parseToLocalDateTime(String dateString) {
        return parseToLocalDateTime(dateString, "yyyy-MM-dd HH:mm:ss");
    }

    private LocalDateTime parseToLocalDateTime(String dateString, String pattern) {
        LocalDateTime localDateTime = null;
        if (isNumber(dateString)) {
            Instant instant = Instant.ofEpochMilli(Long.parseLong(dateString)); // 创建 Instant 对象
            localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); // 转换为 LocalDateTime
        } else {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            localDateTime = LocalDateTime.parse(dateString, formatter);
        }

        return localDateTime;
    }

    private static boolean isNumber(String str) {
        return str != null && str.matches("-?\\d+(\\.\\d+)?"); // 正则表达式匹配整数或小数
    }
}
